package com.example.instantmessage.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.instantmessage.ChatActivity;
import com.example.instantmessage.DateUtils;
import com.example.instantmessage.R;
import com.tencent.imsdk.v2.V2TIMCallback;
import com.tencent.imsdk.v2.V2TIMConversation;
import com.tencent.imsdk.v2.V2TIMManager;
import com.tencent.imsdk.v2.V2TIMMessage;
import com.tencent.imsdk.v2.V2TIMValueCallback;

import java.util.ArrayList;
import java.util.List;

public class MessageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<V2TIMConversation> messageList = new ArrayList<>();
    private Context context;
    private static final String TAG = "MessageAdapter";

    public MessageAdapter(Context context,List<V2TIMConversation> messageList) {
        this.context = context;
        this.messageList = messageList;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.message_item, parent, false);
        MessageViewHolder messageViewHolder = new MessageViewHolder(view);
        return messageViewHolder;
//        return new MessageViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        V2TIMConversation conversation = messageList.get(position);
        MessageViewHolder messageViewHolder = (MessageViewHolder) holder;//???holder是onBindViewHolder（）的参数
//        View view = null;
//        MessageViewHolder messageViewHolder = new MessageViewHolder(view);

        messageViewHolder.friendsId.setText(conversation.getUserID());
        messageViewHolder.friendsChat.setText(conversation.getLastMessage().getTextElem().getText());
        messageViewHolder.chattingTime.setText(DateUtils.getDateToString(conversation.getLastMessage().getTimestamp()*1000));//???
        Log.i(TAG, "onBindViewHolder: " + conversation.getLastMessage().getTimestamp());

        messageViewHolder.messageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, ChatActivity.class);//???
                intent.putExtra("extra_data",conversation.getUserID());
                context.startActivity(intent);
            }
        });

        messageViewHolder.messageView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                V2TIMManager.getConversationManager().pinConversation(String.format("c2c_%s", conversation.getUserID()), true, new V2TIMCallback() {
                    @Override
                    public void onSuccess() {
                        Toast.makeText(view.getContext(),"置顶成功",Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onError(int code, String desc) {

                    }
                });
                return false;
            }
        });

    }

    @Override
    public int getItemCount() {
        return messageList.size();
    }


    static class MessageViewHolder extends RecyclerView.ViewHolder {//icon????
        public ImageView friendsIcon;
        public TextView friendsId, friendsChat, chattingTime;
        public View messageView;

        public MessageViewHolder(View view) {//???
            super(view);
            friendsIcon = view.findViewById(R.id.friends_icon_message);
            friendsId = view.findViewById(R.id.friends_id_message);
            friendsChat = view.findViewById(R.id.friends_chat_message);
            chattingTime = view.findViewById(R.id.chatting_time_message);
            messageView = view;
        }
    }




}
